<!--定金订单管理页面-->
<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.sn" placeholder="定金单号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getDepositOrders">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="info" v-on:click="getAllDepositOrders">清空查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="depositOrders" highlight-current-row stripe border v-loading="listLoading" @selection-change="selsChange" style="width: 100%">
			<el-table-column type="selection" width="50px">
			</el-table-column>
			<el-table-column type="index" width="70px" label="编号">
				<!--自定义编号-->
				<template scope="scope">
					<span>{{(page - 1) * 10 + scope.$index + 1}}</span>
				</template>
			</el-table-column>
			<el-table-column prop="sn" label="定金单号" width="170px">
			</el-table-column>
			<el-table-column   label="签订时间" width="180px" sortable>
				<!--使用过滤器的写法 scope.row.字段名-->
				<template slot-scope="scope">
					<span>{{ scope.row.signTime | formatDate('yyyy-MM-dd HH:mm:ss') }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="sum" label="定金金额" width="105px" sortable>
			</el-table-column>
			<el-table-column prop="customer.name" label="客户" width="90px">
			</el-table-column>
			<el-table-column prop="seller.username" label="营销人员" width="90px">
			</el-table-column>
			<el-table-column prop="intro" label="摘要" width="100px">
			</el-table-column>
			<el-table-column prop="tenant" label="所属租户" width="110px" sortable>
			</el-table-column>
			<el-table-column label="操作" fixed="right" width="280px" align="center">
				<template scope="scope">
					<el-button type="success" size="small" @click="handleCreateContract(scope.$index, scope.row)">生成合同</el-button>
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--分页工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="total,prev, pager, next,jumper" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增合同的界面-->
		<el-dialog title="生成合同" :visible.sync="addContractFormVisible" :close-on-click-modal="false">
			<el-form :model="addContractForm" label-width="80px" :rules="addContractFormRules" ref="addContractForm">
				<!--格式化时间，可以选择日期和时分秒-->
				<el-form-item label="签订时间" prop="signTime" required>
					<el-date-picker type="datetime" placeholder="请选择时间"
									v-model="addContractForm.signTime"
									format="yyyy-MM-dd HH:mm:ss"/>
				</el-form-item>
				<el-form-item label="合同客户" required prop="customerId">
					<el-select v-model="addContractForm.customerId" @change="selectCustomerChange" filterable placeholder="请选择" disabled>
						<el-option
								v-for="item in selectCustomerList"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="营销人员">
					<el-select v-model="addContractForm.sellerId" @change="selectSellerChange" filterable placeholder="请选择" >
						<el-option
								v-for="item in selectSellerList"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="总金额" prop="sum">
					<el-input v-model.number="addContractForm.sum"></el-input>
				</el-form-item>
				<el-form-item label="合同摘要">
					<el-input v-model="addContractForm.intro"></el-input>
				</el-form-item>
				<el-form-item label="所属租户">
					<el-input v-model="addContractForm.tenant" disabled></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addContractFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addContractSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<!--<el-form-item label="定金单号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>-->
				<!--格式化时间，可以选择日期和时分秒-->
				<el-form-item label="签订时间" prop="signTime" required>
					<el-date-picker type="datetime" placeholder="请选择时间"
							v-model="editForm.signTime"
							format="yyyy-MM-dd HH:mm:ss"/>
				</el-form-item>
				<el-form-item label="客户" required prop="customerId">
					<el-select v-model="editForm.customerId" @change="selectCustomerChange2" filterable placeholder="请选择" >
						<el-option
								v-for="item in selectCustomerList"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="营销人员">
					<el-select v-model="editForm.sellerId" @change="selectSellerChange2" filterable placeholder="请选择" >
						<el-option
								v-for="item in selectSellerList"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="定金金额" prop="sum">
					<el-input type="sum" v-model.number="editForm.sum"></el-input>
				</el-form-item>
				<el-form-item label="摘要">
					<el-input v-model="editForm.intro"></el-input>
				</el-form-item>
				<el-form-item label="所属租户">
					<el-input v-model="editForm.tenant"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<!--<el-form-item label="定金单号" prop="sn">
					<el-input v-model="addForm.sn" auto-complete="off"></el-input>
				</el-form-item>-->
				<!--格式化时间，可以选择日期和时分秒-->
				<el-form-item label="签订时间" prop="signTime" required>
					<el-date-picker type="datetime" placeholder="请选择时间"
									v-model="addForm.signTime"
									format="yyyy-MM-dd HH:mm:ss"/>
				</el-form-item>
				<el-form-item label="客户" required prop="customerId">
					<el-select v-model="addForm.customerId" @change="selectCustomerChange" filterable placeholder="请选择" >
						<el-option
								v-for="item in selectCustomerList"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="营销人员">
					<el-select v-model="addForm.sellerId" @change="selectSellerChange" filterable placeholder="请选择" >
						<el-option
								v-for="item in selectSellerList"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="定金金额" prop="sum">
					<el-input v-model.number="addForm.sum"></el-input>
				</el-form-item>
				<el-form-item label="摘要">
					<el-input v-model="addForm.intro"></el-input>
				</el-form-item>
				<el-form-item label="所属租户">
					<el-input v-model="addForm.tenant"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	/*git测试,增加此注释*/
	import {batchRemoveUser} from "../../api/api";

	export default {
		data() {
			return {
				filters: {
					sn: '',
					sum:''
				},
				depositOrders: [],
				depositOrder:'',
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				//生成合同相关
				addContractFormVisible:false,
				addContractFormRules: {
					sum:[{
						required: true,
						message: '请输入定金'
					},
						{
							type: 'number',
							message: '金额必须为数字值'
						},

					],
					signTime: { type: 'date', required: true, message: '请选择时间', trigger: 'change' },
					customerId:{ required: true, message: '请选择客户', trigger: 'blur' }
				},
				//生成合同界面数据
				addContractForm: {
					/*sn: '',*/
					signTime:'',
					sum:'',
					intro:'',
					tenant:'',
					customerId:'',
					sellerId:''
				},


				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					/*sn: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					],*/
					//前台会显示类型不对报错
					sum:[{
						required: true,
						message: '请输入定金'
					},
						{
							type: 'number',
							message: '定金必须为数字值'
						},

					],
					signTime:{ type: 'date', required: true, message: '请选择时间', trigger: 'change' },
					customerId:{ required: true, message: '请选择客户', trigger: 'blur' }
				},
				//编辑界面数据
				editForm: {
					id: 0,
					/*sn: '',*/
					signTime:'',
					sum:'',
					intro:'',
					tenant:'',
					customerId:'',
					sellerId:''
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					sn: [
						{ required: true, message: '请输入定金单号', trigger: 'blur' }
					],
					sum:[{
						required: true,
						message: '请输入定金'
					},
						{
							type: 'number',
							message: '定金必须为数字值'
						},

					],
					signTime: { type: 'date', required: true, message: '请选择时间', trigger: 'change' },
					customerId:{ required: true, message: '请选择客户', trigger: 'blur' }
				},
				//新增界面数据
				addForm: {
					/*sn: '',*/
					signTime:'',
					sum:'',
					intro:'',
					tenant:'',
					customerId:'',
					sellerId:''
				}

			}
		},
		computed: {

			selectCustomerList() {//初始化下拉框动态数据
				let obj = [];
				this.$http.patch("/customer/list").then(res => {
					res.data.forEach((item, index) => {//关键的是将后台返回的数据进行遍历，并封装好
						obj.push({
							id: item.id,//id必须品啊
							name: item.name//name必须品啊
						});
					});
				});
				return obj;
			},
			selectSellerList() {//初始化下拉框动态数据
				let obj = [];
				this.$http.patch("/employee/list").then(res => {
					res.data.forEach((item, index) => {//关键的是将后台返回的数据进行遍历，并封装好
						obj.push({
							id: item.id,//id必须品啊
							name: item.username//name必须品啊
						});
					});
				});
				return obj;
			},
		},
		methods: {
			//获取销售人员下拉数据,添加数据
			selectSellerChange(val){
				let obj = {};
				obj = this.selectSellerList.find(function(item) {//obj是选中的对象
					return item.id === val;
				});
				/*this.addForm.id = obj.id;//提交数据使用，此处可忽略
				this.addForm.name = obj.name;//用于回显名称*/
			},
			//获取销售人员下拉数据,编辑数据
			selectSellerChange2(val){
				//出现这个问题好像是因为下拉框数据是循环掉别的接口得来的，
				// 因为数据层次太多，render函数没有自动更新，
				// 需手动强制刷新所以我直接强制刷新了值，而forceUpdate就是重新render。
				this.$forceUpdate();
				let obj = {};
				obj = this.selectSellerList.find(function(item) {//obj是选中的对象
					return item.id === val;
				});

			},
			//获取客户下拉数据,添加数据
			selectCustomerChange(val){
				let obj = {};
				obj = this.selectCustomerList.find(function(item) {//obj是选中的对象
					return item.id === val;
				});
				/*this.addForm.id = obj.id;//提交数据使用，此处可忽略
				this.addForm.name = obj.name;//用于回显名称
				console.log(this.addForm.customerId)*/
			},
			//获取客户下拉数据,编辑数据
			selectCustomerChange2(val){
				//出现这个问题好像是因为下拉框数据是循环掉别的接口得来的，
				// 因为数据层次太多，render函数没有自动更新，
				// 需手动强制刷新所以我直接强制刷新了值，而forceUpdate就是重新render。
				this.$forceUpdate();
				let obj = {};
				obj = this.selectCustomerList.find(function(item) {//obj是选中的对象
					return item.id === val;
				});

			},

			//分页信息
			handleCurrentChange(val){
				this.page = val;
				this.getDepositOrders();
			},
			getAllDepositOrders(){
				this.filters.sn = '';
				this.getDepositOrders();
			},
			//获取用户列表
			getDepositOrders() {
				let param = {
					"currentPage":this.page,
					"sn":this.filters.sn
				}

				this.listLoading = true;
				this.$http.patch('/depositOrder/queryPage',param).then((res) => {
					this.depositOrders = res.data.rows;
					this.total = res.data.total;
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete('/depositOrder/delete/'+row.id).then((res) => {
						let {success,msg} = res.data;
						this.listLoading = false;
						if (success){
							this.$message({
								message: msg,
								type: 'success'
							});
							this.getDepositOrders();
						}else{
							this.$message({
								message: msg,
								type: 'error'
							});
						}
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
				//修改页面回显客户
				this.editForm.customerId = row.customer.id;
				this.editForm.sellerId = row.seller.id;
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;

			},
			//打开创建合同的窗体
			handleCreateContract(index,row){
				//点击生成合同时，先查询数据库，该合同是否已经存在，存在则进行提示
				this.$http.get("/contract/findByDepositId/"+row.id).then(res =>{
					if (res.data.success){
						this.$message({
							message: "合同已经存在,请在合同管理查看",
							type: 'warning'
						});
					} else {//不存在则创建
						this.addContractFormVisible = true;
						//回显定金订单的部分信息到合同上面
						this.addContractForm.intro = row.intro;
						this.addContractForm.tenant = row.tenant;
						if ( row.customer && row.customer.name){
							this.addContractForm.customerId = row.customer.id;
						}
						if (row.seller && row.seller.username){
							this.addContractForm.sellerId = row.seller.id;
						}
						this.depositOrder = row;
					}
				});

			},
			//生成合同
			addContractSubmit: function () {
				this.$refs.addContractForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addContractForm);
							//将合同id绑定到合同对象中
							para.customer = {
								"id":this.addContractForm.customerId
							};
							//封装销售人员id
							para.seller = {
								"id":this.addContractForm.sellerId
							};
							para.depositOrder = this.depositOrder;

							this.$http.put('/contract/save',para).then((res) => {
								/*解构*/
								let {success,msg} = res.data;
								this.addLoading = false;
								//判断是否成功
								if (success){
									this.$message({
										message: msg,
										type: 'success'
									});
									/*清空表单*/
									this.$refs['addContractForm'].resetFields();
									this.addContractFormVisible = false;
									/*不需要刷新数据*/
									/*this.getDepositOrders();*/
								}else {
									this.$message({
										message: msg,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},



			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							//封装客户id，以便传入后台
							para.customer = {
								"id":this.editForm.customerId
							};
							//封装销售人员id
							para.seller = {
								"id":this.editForm.sellerId
							};
							/*//如果表单日期没有填写，则默认为当前时间
							if (!this.editForm.signTime){
								this.editForm.signTime = new Date();
							}*/
							this.$http.post('/depositOrder/update',para).then((res) => {
								let {success,msg} = res.data;
								this.editLoading = false;
								if (success){
									this.$message({
										message: msg,
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getDepositOrders();
								}else{
									this.$message({
										message: msg,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							//将客户id绑定到定金订单对象中
							para.customer = {
								"id":this.addForm.customerId
							};
							//封装销售人员id
							para.seller = {
								"id":this.addForm.sellerId
							};
							/*//如果表单日期没有填写，则默认为当前时间
							if (!this.addForm.signTime){
								para.signTime = new Date();
							}*/
							this.$http.put('/depositOrder/save',para).then((res) => {
								/*解构*/
								let {success,msg} = res.data;
								this.addLoading = false;
								//判断是否成功
								if (success){
									this.$message({
										message: msg,
										type: 'success'
									});
									/*清空表单*/
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									/*刷新数据*/
									this.getDepositOrders();
								}else {
									this.$message({
										message: msg,
										type: 'error'
									});
								}
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { "ids": ids };
					this.$http.post("/depositOrder/batchDelete",para).then((res) => {
						let {success,msg} = res.data;
						this.listLoading = false;
						if (success){
							this.$message({
								message: msg,
								type: 'success'
							});
							this.getDepositOrders();
						}else{
							this.$message({
								message: msg,
								type: 'error'
							});
						}
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getDepositOrders();
		}
	}

</script>

<style scoped>

</style>